const Koa = require('koa');

console.log(Koa);

const app = new Koa();

const static = require('koa-static');

const Router = require('koa-router');

const router = new Router();

const path = require('path');

const fs = require('fs');

const query = require('./db/db');

app.use(static(path.join(__dirname,'./public')));

app.use(router.routes());

app.use(router.allowedMethods());

//1.每次请求花费的时间-请求地址-请求方式  输出到当前的.log文件内

// app.use(async (ctx,next) => {
//     let startTime = new Date().getTime();
//     console.log("第一个中间件开始");
//     next();
//     console.log("第一个中间件结束");
//     let endTime = new Date().getTime();
//     let time = endTime - startTime;
//     fs.appendFileSync('./log.log',`本地请求花费的时间${time}-${ctx.url}-${ctx.method}`)
//     ctx.body = `本地请求花费的时间${time}`;
// })

// app.use(async (ctx,next) => {
//     console.log("第二个中间件开始");
//     next();
//     console.log("第二个中间件结束")
// })

router.get('/api/list',async (ctx,next) => {

    let list = await query('select * from userlist');
    ctx.body = {
        code:1,
        data:list
    }
})

router.get('/api/detail',async (ctx,body) => {
    ctx.body = {detail:'detail'}
})

router.post('/api/add',async (ctx,body ) => {
    ctx.body = {'type':'add'}
})

app.listen(3000,() => {
    console.log("当前服务的端口号是3000")
})